{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Modules for data processing\n",
    "import numpy as np\n",
    "import os\n",
    "from netCDF4 import Dataset\n",
    "import scipy.io as sio\n",
    "import xlrd\n",
    "\n",
    "# Modules for plotting\n",
    "from ipywidgets import *\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "import bokeh.io\n",
    "import bokeh.mpl\n",
    "import bokeh.plotting\n",
    "bokeh.io.output_notebook()\n",
    "import seaborn as sns\n",
    "sns.set_style(style='white')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Option 1: Load data from an Excel file\n",
    "<pre>\n",
    "workbook = xlrd.open_workbook(<span style=\"color:#cc7a00\"><b>'EXCEL_FILE.xlsx'</b></span>)\n",
    "sheet_names = workbook.sheet_names()\n",
    "sheet = workbook.sheet_by_name(sheet_names[i])\n",
    "</pre>\n",
    "\n",
    "#### Option 2: Load data from a Matlab file\n",
    "<pre>\n",
    "matfile  = sio.loadmat(<span style=\"color:#cc7a00\"><b>'MATLAB_FILE.mat'</b></span>)\n",
    "data = matfile[<span style=\"color:#cc7a00\"><b>'VARIABLE_TOPOGRAPHY'</b></span>]\n",
    "</pre>\n",
    "\n",
    "#### Option 3: Load data from a netCDF file\n",
    "<pre>\n",
    "nc_fid = Dataset(<span style=\"color:#cc7a00\"><b>'NETCDF_FILE.nc'</b></span>, 'r')\n",
    "data = nc_fid.variables[<span style=\"color:#cc7a00\"><b>VARIABLE_TOPOGRAPHY</b></span>][:]\n",
    "</pre>\n",
    "\n",
    "#### Run the command below to create forcing for Dhara model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Load matlab file\n",
    "matfile  = sio.loadmat('flux_data.mat')\n",
    "\n",
    "# Load variables\n",
    "LAI_in = matfile['LAI_in']\n",
    "LAD_in = np.array([\n",
    "        0.0010, 0.0337, 0.0661, 0.0610, 0.0575,\n",
    "        0.0575, 0.0660, 0.0644, 0.0629, 0.0720,\n",
    "        0.0891, 0.1110, 0.1395, 0.0945, 0.0250])\n",
    "rootfr_in = np.array([\n",
    "        0.8100, 0.1000, 0.0400, 0.0200, 0.0130, \n",
    "        0.0070, 0.0040, 0.0030, 0.0020, 0.0001, \n",
    "        0.0001, 0.0001, 0.0001, 0.0001, 0.0001])\n",
    "\n",
    "nl_can_in    = len(LAD_in)\n",
    "nl_soil_in   = len(rootfr_in)\n",
    "num_steps_in = len(LAI_in)\n",
    "\n",
    "# Check/create folder and topography file name\n",
    "folder = 'data'\n",
    "filename_output = 'soybean.nc'\n",
    "if not os.path.exists(folder):\n",
    "    os.makedirs(folder)\n",
    "\n",
    "################################################################\n",
    "# DO NOT CHANGE BELOW THIS LINE --------------------------------\n",
    "################################################################\n",
    "\n",
    "plant = Dataset(folder + '/' + filename_output, 'w', format='NETCDF4')\n",
    "plant.description = 'Vegetation data for Dhara'\n",
    "\n",
    "# Set up dimension\n",
    "plant.createDimension('canopy', nl_can_in)  # canopy variable\n",
    "plant.createDimension('soil', nl_soil_in)   # soil variable\n",
    "plant.createDimension('time', num_steps_in) # time variable\n",
    "plant.createDimension('scalar', 1)          # Scalar variable\n",
    "\n",
    "\"\"\"\n",
    "Create variables in the netcdf file\n",
    "    var = netcdf.createVariable('Var_name', 'var_type', ('dimension_type'))\n",
    "\"\"\"\n",
    "LAI       = plant.createVariable('LeafAreaIndex', 'f8', ('time'))\n",
    "LAD       = plant.createVariable('LeafAreaDensity', 'f8', ('canopy'))\n",
    "rootfr    = plant.createVariable('RootFraction', 'f8', ('soil'))\n",
    "nl_can    = plant.createVariable('NumCanoyLayers', 'i4', ('scalar'))\n",
    "nl_soil   = plant.createVariable('NumSoilLayers', 'i4', ('scalar'))\n",
    "num_steps = plant.createVariable('NumTimeSteps', 'i4', ('scalar'))\n",
    "\n",
    "\n",
    "# Assign data to variables in NetCDF file\n",
    "LAI[:]       = LAI_in\n",
    "LAD[:]       = LAD_in\n",
    "rootfr[:]    = rootfr_in\n",
    "nl_can[:]    = nl_can_in\n",
    "nl_soil[:]   = nl_soil_in\n",
    "num_steps[:] = num_steps_in\n",
    "\n",
    "# Close the file\n",
    "plant.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### OPTIONAL -- If you want to visualize canopy and root profiles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ylad = np.linspace(1,nl_can_in,nl_can_in)\n",
    "ysoil = np.linspace(1,nl_soil_in,nl_soil_in)\n",
    "\n",
    "fig, ax = plt.subplots(ncols=2, nrows=1, figsize=(10,5))        \n",
    "ax[0].plot(LAD_in, ylad/nl_can_in)\n",
    "ax[0].set_xlabel('Leaf Area Density')\n",
    "ax[0].set_ylabel('z/h')\n",
    "ax[0].grid(True)\n",
    "ax[1].plot(rootfr_in, -ysoil/nl_soil_in)\n",
    "ax[1].set_xlabel('Root fraction')\n",
    "ax[1].set_ylabel('z/h')\n",
    "ax[1].grid(True)\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### and Leaf Area Index data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Plotting data\n",
    "fig, ax = plt.subplots(figsize=(9,4))\n",
    "ax.plot(LAI_in)\n",
    "ax.set_xlabel('Time')\n",
    "ax.set_ylabel('LAI')\n",
    "ax.grid(True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.4.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
